package com.farm.mapper;

import com.farm.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @FileName AddressBookMapper
 * @Description
 * @Author Sword
 * @date 2025-02-28
 **/

@Mapper
public interface AddressBookMapper {

    /**
     * 新增地址
     *
     * @param addressBook
     */
    @Insert("insert into address_book (user_id, consignee, sex, phone, " +
            "province_code, province_name, city_code, city_name, district_code, district_name," +
            " detail, label, is_default) " +
            "VALUES (#{userId},#{consignee},#{sex},#{phone}," +
            "#{provinceCode},#{provinceName},#{cityCode},#{cityName},#{districtCode},#{districtName}," +
            "#{detail},#{label},#{isDefault})")
    void insert(AddressBook addressBook);

    /**
     * 查询当前登录用户的所有地址信息
     *
     * @param addressBook
     * @return
     */
    @Select("select * from address_book where user_id = #{userId};")
    List<AddressBook> list(AddressBook addressBook);

    /**
     * 根据id修改地址
     *
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @Select("select * from address_book where id = #{id};")
    AddressBook getById(Long id);

    /**
     * 根据id删除地址
     *
     * @param id
     */
    @Delete("delete from address_book where id = #{id};")
    void delete(Long id);

    /**
     * 根据id修改默认地址值
     *
     * @param addressBook
     */
    @Update("update address_book set is_default = #{isDefault} where user_id = #{userId}")
    void updateDefaultByuserId(AddressBook addressBook);

    /**
     * 查询默认地址
     *
     * @param addressBook
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} and is_default = #{isDefault};")
    AddressBook getDefault(AddressBook addressBook);
}
